﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace WebHousingInfo
{
    public partial class BaiVietDienDan : System.Web.UI.Page
    {
        private static string mode = null, modeback = null, modeidsub = null;
        private static ForumNewsBO forumnewsbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                modeidsub = Request.QueryString["idsub"];
                mode = Request.QueryString["id"];
                modeback = Request.QueryString["goback"];
                if (mode != null)
                {
                    btndangtin.Text = "Chỉnh sửa bài viết";
                    forumnewsbo = ForumNewsBO.GetForumNewsBO(Convert.ToInt32(mode));
                    txttieude.Text = forumnewsbo.Title;
                    txtnoidung.Text = forumnewsbo.ForumNewsContent;
                }
            }
            
        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            if (mode != null)
            {
                int forumnewid = Convert.ToInt32(mode);
                forumnewsbo = ForumNewsBO.GetForumNewsBO(forumnewid);
                forumnewsbo.Title = txttieude.Text;
                forumnewsbo.ForumNewsContent = txtnoidung.Text;
                forumnewsbo.UpdatedDate = DateTime.Now;
                try
                {
                    forumnewsbo.Save();
                    goback();
                }
                catch
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Cập nhật thất bại !!!')", true);
                    return;
                }
            }
            else
            {
                forumnewsbo = ForumNewsBO.NewForumNewsBO();
                forumnewsbo.UserName = "admin";
                forumnewsbo.Title = txttieude.Text;
                forumnewsbo.SubTopicForumID = Convert.ToInt32(modeidsub);
                forumnewsbo.CreatedDate = DateTime.Now;
                forumnewsbo.UpdatedDate = DateTime.Now;
                forumnewsbo.ForumNewsContent = txtnoidung.Text;
                forumnewsbo.ViewCount = 0;
                try
                {
                    forumnewsbo.Save();
                    Response.Redirect("DienDanTinTheoChuDe.aspx?idsub=" + modeidsub);
                }
                catch
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đăng tin thất bại !!!')", true);
                    return;
                }
            }
        }

        private void goback()
        {
            try
            {
                var splitmode = modeback.Split('-');
                Response.Redirect("DienDanBinhLuan.aspx?categoryid=" + splitmode[0] + "&id=" + splitmode[1]);
            }
            catch { }
        }
    }
}
